package com.hivekion.quartz.controller;

import com.hivekion.common.annotation.AutoLog;
import com.hivekion.common.entity.PagedResultVo;
import com.hivekion.common.enums.OperationTypeEnum;
import com.hivekion.quartz.domain.vo.*;
import com.hivekion.quartz.service.IQuartzService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @authot morning
 * @date 2021/8/4 10:18
 */
@RestController
@RequestMapping("/quartz")
@Api(tags = "任务调度")
public class QuartzController {

      @Autowired
      private IQuartzService quartzService;

      @PostMapping("/queryList")
      @ApiOperation(value = "查询数据", notes = "查询数据")
      @AutoLog(value = "查看定时任务记录", operationType = OperationTypeEnum.SELECT, module = "定时任务")
      public PagedResultVo<JobViewVo> queryList(@RequestBody JobSearchPageVo pageVo) {
	    return quartzService.queryList(pageVo);
      }

      @PostMapping("/add")
      @PreAuthorize("@Permission.hasPermi('pro:quartz:add')")
      @AutoLog(value = "新增定时任务", operationType = OperationTypeEnum.INSERT, module = "定时任务")
      public String addJob(@RequestBody JobCreateInputVo job) {
	    job.setCronExpression("0 */1 * * * ?");
	    Boolean result = quartzService.addJob(job);
	    if (!result) {
		  return "创建定时任务失败";
	    }
	    return "创建定时任务成功:" + job.getJobName();
      }

      @PutMapping("/update")
      @PreAuthorize("@Permission.hasPermi('pro:quartz:edit')")
      @AutoLog(value = "修改任务", operationType = OperationTypeEnum.UPDATE, module = "定时任务")
      @ApiOperation(value = "修改任务，修改开关无效，修改开关需要发启/停请求", notes = "修改任务")
      public Boolean updateJob(@RequestBody JobUpdateInputVo job) {
	    return quartzService.updateJob(job);
      }

      @GetMapping("/pause")
      @PreAuthorize("@Permission.hasPermi('pro:quartz:pause')")
      @AutoLog(value = "暂停任务", operationType = OperationTypeEnum.UPDATE, module = "定时任务")
      @ApiOperation(value = "暂停任务,参数格式{id:id}", notes = "暂停任务")
      public Boolean pauseJob(String id) {
	    return quartzService.pauseJob(id);
      }

      @GetMapping("/resume")
      @PreAuthorize("@Permission.hasPermi('pro:quartz:resume')")
      @AutoLog(value = "唤醒任务", operationType = OperationTypeEnum.UPDATE, module = "定时任务")
      @ApiOperation(value = "唤醒任务,参数格式{id:id}", notes = "唤醒任务")
      public Boolean resumeJob(String id) {
	    return quartzService.resumeJob(id);
      }

      @DeleteMapping("/delete")
      @PreAuthorize("@Permission.hasPermi('pro:quartz:remove')")
      @AutoLog(value = "删除任务", operationType = OperationTypeEnum.UPDATE, module = "定时任务")
      @ApiOperation(value = "删除任务,参数格式{id:id}", notes = "删除任务")
      public boolean deleteJob(String id) {
	    return quartzService.deleteJob(id);
      }

      @DeleteMapping("/removes")
      @PreAuthorize("@Permission.hasPermi('pro:quartz:remove')")
      @ApiOperation(value = "批量删除", notes = "删除任务")
      @AutoLog(value = "删除任务", operationType = OperationTypeEnum.UPDATE, module = "定时任务")
      public boolean removes(@RequestBody List<String> ids) {
	    return quartzService.removes(ids);
      }

      @GetMapping("/getRunJobList")
      public List<JobRunViewVo> getRunJobList() {
	    return quartzService.queryListOpen();
      }

      @GetMapping("/getRunningJobList")
      public List<JobRunViewVo> getRunningJobList() {
	    return quartzService.queryRuningList();
      }

      @GetMapping("/getInfo")
      public JobModelVo getInfo(String id) {
	    return quartzService.getInfo(id);
      }
}
